import Vue from "vue";
import notify from "./src/notification.vue";

const Notification = Vue.extend(notify);

let instances = [];
const AsNotification = (options, message, type, duration, icon) => {
  if (options === null || options === undefined) {
    options = {
      message: "这是一段内容"
    };
  } else if (typeof options === "string" || typeof options === "number") {
    options = {
      title: "标题",
      message: options
    };
  }

  if (duration) {
    options.duration = duration;
  }
  if (type) {
    options.type = type;
  }
  if (icon) {
    options.icon = icon;
  }
  if (message) {
    options.message = message;
  }

  options.visible = false;
  options.instances = instances;
  options.top = 0;

  const instance = new Notification({ data: options }).$mount();
  instances.push(instance);

  document.body.appendChild(instance.$el);
};

["success", "error", "warning", "info"].forEach(item => {
  AsNotification[item] = options => {
    // options.type = item;
    if (typeof options === "string" || typeof options === "number") {
      options = {
        title: item,
        message: options
      };
    }
    options.type = item;
    return AsNotification(options);
  };
});

export default AsNotification;
